package com.zpz.framework.zpzoauth.common.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.zpz.framework.zpzoauth.common.encrypt.ZpzOauthSaltUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.apache.tomcat.util.codec.binary.Base64;

import java.io.Serializable;
import java.lang.reflect.Field;

/**
 * @author zhangpanzhi
 * @time 2019-07-09
 * @description 未经本人允许请勿随便改动，尊重劳动
 * */
@ApiModel(value = "返回值", description = "统一返回值封装类")
@JsonInclude(JsonInclude.Include.NON_NULL) // 不输出NULL字段
public class ZpzOauthResultMsg<T> implements Serializable {
	private static final long serialVersionUID = 1L;
	@ApiModelProperty(value = "应答码")
    private int code;
	@ApiModelProperty(value = "应答码描述,返回给用户的提示在这里")
    private String message;
	@ApiModelProperty(value = "错误消息,如果系统运行出错才有值,给程序员用")
	private String errorDesc ="";
	private boolean de;
	private boolean vsg;
	private String sign;
	private String noncestr;
	private String timestamp;

	public String getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(String timestamp) {
		this.timestamp = timestamp;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public boolean isDe() {
		return de;
	}

	public void setDe(boolean de) {
		this.de = de;
	}

	public boolean isVsg() {
		return vsg;
	}

	public void setVsg(boolean vsg) {
		this.vsg = vsg;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	public String getNoncestr() {
		return noncestr;
	}

	public void setNoncestr(String noncestr) {
		this.noncestr = noncestr;
	}

	@ApiModelProperty(value = "应答数据体")
    private T data;

	public ZpzOauthResultMsg() {
		super();
	}

	public ZpzOauthResultMsg(int code, String message, String errorDesc, boolean de, boolean vsg, String sign, String noncestr, String timestamp, T data) {
		this.code = code;
		this.message = message;
		this.errorDesc = errorDesc;
		this.de = de;
		this.vsg = vsg;
		this.sign = sign;
		this.noncestr = noncestr;
		this.data = data;
		this.timestamp = timestamp;
	}

	public ZpzOauthResultMsg(int code, String message, String errorDesc, T data) {
		this.code = code;
		this.message = message;
		this.errorDesc = errorDesc;
		this.data = data;
	}

	/**
	 * zhangpanzhi
	 * 通用返回成功,不带数据体
	 * @return
	 */
	public static <T> ZpzOauthResultMsg<T> ok(){
		return new ZpzOauthResultMsg<T>(ZpzOauthResultStatus.OK.getCode(), ZpzOauthResultStatus.OK.getMessage(), "", false, false, "", "","", null);
	}
	public static <T> ZpzOauthResultMsg<T> ok(boolean de,boolean vsg,String sign,String noncestr,String timestamp){
		return new ZpzOauthResultMsg<T>(ZpzOauthResultStatus.OK.getCode(), ZpzOauthResultStatus.OK.getMessage(), "", de, vsg, sign, noncestr,timestamp, null);
	}
	/**
	 * zhangpanzhi
	 * 通用返回成功,带数据体
	 * @param data
	 * @return
	 */
	public static <T> ZpzOauthResultMsg<T> okForData(T data){
		return new ZpzOauthResultMsg<T>(ZpzOauthResultStatus.OK.getCode(), ZpzOauthResultStatus.OK.getMessage(), "", false, false, "", "","", data);
	}
	public static <T> ZpzOauthResultMsg<T> okForData(T data,boolean de,boolean vsg,String sign,String noncestr,String timestamp){
		return new ZpzOauthResultMsg<T>(ZpzOauthResultStatus.OK.getCode(), ZpzOauthResultStatus.OK.getMessage(), "", de, vsg, sign, noncestr,timestamp, data);
	}

	/**
	 * zhangpanzhi
	 * 用于请求参数校验失败，调用
	 * 通用返回失败,不带数据体
	 * @return
	 */
	public static <T> ZpzOauthResultMsg<T> error(String message){
		return new ZpzOauthResultMsg<T>(ZpzOauthResultStatus.EXCEPTION.getCode(), message, "", false, false, "", "","", null);
	}
	/**
	 * zhangpanzhi
	 * 用于请求参数校验失败，调用
	 * 通用返回失败,不带数据体
	 * @return
	 */
	public static <T> ZpzOauthResultMsg<T> error(){
		return new ZpzOauthResultMsg<T>(ZpzOauthResultStatus.EXCEPTION.getCode(), ZpzOauthResultStatus.EXCEPTION.getMessage(), "", false, false, "", "","", null);
	}

//	/**
//	 * zhangpanzhi
//	 * 用于请求参数校验失败，调用
//	 * 通用返回失败,自定义消息,不带数据体
//	 * @param errorDesc 错误详情
//	 * @return
//	 */
//	public static <T> ZpzOauthResultMsg<T> error(String errorDesc){
//		return new ZpzOauthResultMsg<T>(ZpzOauthResultStatus.EXCEPTION.getCode(), ZpzOauthResultStatus.EXCEPTION.getMessage(), errorDesc, false, false, "", "","", null);
//	}


	/**
	 * zhangpanzhi
	 * 业务自定义：返回ResultStatus
	 * @return
	 */
	public static <T> ZpzOauthResultMsg<T> customResultStatus(Enum<?> resultStatus){
		int code= ZpzOauthResultStatus.CODE_FORM_ERROR.getCode();
		String message= ZpzOauthResultStatus.CODE_FORM_ERROR.getMessage();
		Class<?> clazz = resultStatus.getDeclaringClass();
		Field[] fields = clazz.getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
			Field f = fields[i];
			f.setAccessible(true); //设置些属性是可以访问的
			try {
				if("code".equals(f.getName())){
					code=(int) f.get(resultStatus);
				}
				if("message".equals(f.getName())){
					message=(String) f.get(resultStatus);
				}
			} catch (IllegalArgumentException e) {
				//打印日志
				return error(e.toString());
			} catch (IllegalAccessException e) {
				//打印日志
				return error(e.toString());
			}

		}
		return new ZpzOauthResultMsg<T>(code,message, "", false, false, "", "","", null);
	}
	public static <T> ZpzOauthResultMsg<T> customResultStatus(Enum<?> resultStatus,boolean de,boolean vsg,String sign,String noncestr,String timestamp){
		int code= ZpzOauthResultStatus.CODE_FORM_ERROR.getCode();
		String message= ZpzOauthResultStatus.CODE_FORM_ERROR.getMessage();
		Class<?> clazz = resultStatus.getDeclaringClass();
		Field[] fields = clazz.getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
			Field f = fields[i];
			f.setAccessible(true); //设置些属性是可以访问的
			try {
				if("code".equals(f.getName())){
					code=(int) f.get(resultStatus);
				}
				if("message".equals(f.getName())){
					message=(String) f.get(resultStatus);
				}
			} catch (IllegalArgumentException e) {
				//打印日志
				return error(e.toString());
			} catch (IllegalAccessException e) {
				//打印日志
				return error(e.toString());
			}

		}
		return new ZpzOauthResultMsg<T>(code,message, "", de, vsg, sign, noncestr,timestamp, null);
	}

	/**
	 * zhangpanzhi
	 * 业务自定义：返回ResultStatus
	 * @param data
	 * @return
	 */
	public static <T> ZpzOauthResultMsg<T> customResultStatus(Enum<?> resultStatus, T data){
		int code= ZpzOauthResultStatus.CODE_FORM_ERROR.getCode();
		String message= ZpzOauthResultStatus.CODE_FORM_ERROR.getMessage();
		Class<?> clazz = resultStatus.getDeclaringClass();
		Field[] fields = clazz.getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
			Field f = fields[i];
			f.setAccessible(true); //设置些属性是可以访问的
			try {
				if("code".equals(f.getName())){
					code=(int) f.get(resultStatus);
				}
				if("message".equals(f.getName())){
					message=(String) f.get(resultStatus);
				}
			} catch (IllegalArgumentException e) {
				//打印日志
				return error(e.toString());
			} catch (IllegalAccessException e) {
				//打印日志
				return error(e.toString());
			}
		}
		return new ZpzOauthResultMsg<T>(code,message, "", false, false, "", "","", data);
	}
	public static <T> ZpzOauthResultMsg<T> customResultStatus(Enum<?> resultStatus, T data,boolean de,boolean vsg,String sign,String noncestr,String timestamp){
		int code= ZpzOauthResultStatus.CODE_FORM_ERROR.getCode();
		String message= ZpzOauthResultStatus.CODE_FORM_ERROR.getMessage();
		Class<?> clazz = resultStatus.getDeclaringClass();
		Field[] fields = clazz.getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
			Field f = fields[i];
			f.setAccessible(true); //设置些属性是可以访问的
			try {
				if("code".equals(f.getName())){
					code=(int) f.get(resultStatus);
				}
				if("message".equals(f.getName())){
					message=(String) f.get(resultStatus);
				}
			} catch (IllegalArgumentException e) {
				//打印日志
				return error(e.toString());
			} catch (IllegalAccessException e) {
				//打印日志
				return error(e.toString());
			}
		}
		return new ZpzOauthResultMsg<T>(code,message, "", de, vsg, sign, noncestr,timestamp, data);
	}

	public static <T> ZpzOauthResultMsg<T> customResultStatus(Enum<?> resultStatus, String errorDesc){
		int code= ZpzOauthResultStatus.CODE_FORM_ERROR.getCode();
		String message= ZpzOauthResultStatus.CODE_FORM_ERROR.getMessage();
		Class<?> clazz = resultStatus.getDeclaringClass();
		Field[] fields = clazz.getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
			Field f = fields[i];
			f.setAccessible(true); //设置些属性是可以访问的
			try {
				if("code".equals(f.getName())){
					code=(int) f.get(resultStatus);
				}
				if("message".equals(f.getName())){
					message=(String) f.get(resultStatus);
				}
			} catch (IllegalArgumentException e) {
				//打印日志
				return error(e.toString());
			} catch (IllegalAccessException e) {
				//打印日志
				return error(e.toString());
			}
		}
		return new ZpzOauthResultMsg<T>(code,message, errorDesc, false, false, "", "","", null);
	}
	public static <T> ZpzOauthResultMsg<T> customResultStatus(Enum<?> resultStatus, String errorDesc,boolean de,boolean vsg,String sign,String noncestr,String timestamp){
		int code= ZpzOauthResultStatus.CODE_FORM_ERROR.getCode();
		String message= ZpzOauthResultStatus.CODE_FORM_ERROR.getMessage();
		Class<?> clazz = resultStatus.getDeclaringClass();
		Field[] fields = clazz.getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
			Field f = fields[i];
			f.setAccessible(true); //设置些属性是可以访问的
			try {
				if("code".equals(f.getName())){
					code=(int) f.get(resultStatus);
				}
				if("message".equals(f.getName())){
					message=(String) f.get(resultStatus);
				}
			} catch (IllegalArgumentException e) {
				//打印日志
				return error(e.toString());
			} catch (IllegalAccessException e) {
				//打印日志
				return error(e.toString());
			}
		}
		return new ZpzOauthResultMsg<T>(code,message, errorDesc, de, vsg, sign, noncestr,timestamp, null);
	}


	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getErrorDesc() {
		return errorDesc;
	}

	public void setErrorDesc(String errorDesc) {
		this.errorDesc = errorDesc;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

    public static void main(String[] args) {
        System.out.println(Base64.encodeBase64String("znyygl_manager_8u7T9u1x0H8myr".getBytes()));
        System.out.println(ZpzOauthSaltUtil.getPwd("znyygl_manager_8u7T9u1x0H8myr"));
    }
}

